package com.yanqu.road.server.cmd.palace;

import com.yanqu.road.pb.estatewar.EstateWarProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.palace.CrossPalaceActivity;
import com.yanqu.road.server.manager.palace.CrossPalaceActivityMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_PALACE_WORSHIP, desc = "膜拜")
public class CrossPalaceWorshipCmd extends CrossCommand {
    private long userId;
    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        EstateWarProto.PalaceWorshipReqMsg reqMsg = EstateWarProto.PalaceWorshipReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossPalaceActivity crossPalaceActivity = CrossPalaceActivityMgr.getCrossPalaceActivity(activityId);
        if(null == crossPalaceActivity){
            ret = GameErrorCode.E_PALACE_ACTIVITY_NO_FOUND;
            return;
        }
        ret = crossPalaceActivity.worship(serverId, userId);
    }

    @Override
    public void sendData() {
        if(0 != ret) {
            EstateWarProto.PalaceWorshipRespMsg.Builder respMsg = EstateWarProto.PalaceWorshipRespMsg.newBuilder();
            respMsg.setRet(ret);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_PALACE_WORSHIP, respMsg);
            MessageHelper.sendPacket(serverId, userId, pbMsg);
        }
    }
}
